﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;

namespace Zor_DAL
{
     public class Zor_Acceptance_DAL<T> where T : class
    {
        /// <summary>
        /// 受理单信息管理-修改
        /// </summary>
        /// <param name="acceptance">受理单信息对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
         public static BaseAnswer UpdateAcceptance(int PropertyTax, int PersonalTax, int Acceptance_Tax, int Acceptance_TotalTax, Zor_Acceptance acceptance, DateTime startTime, DateTime endTime)
        {
            var sql = "";
            var st = TimeHelper.ToTimeStamp(startTime);   //税起时间转时间戳格式
            var et = TimeHelper.ToTimeStamp(endTime);     //税止时间转时间戳格式

            //修改受理单表中的数据（房产税，个人所得税，计税总额等信息）
            sql += "update [Zor_Acceptance]  set Acceptance_PropertyTax =@Acceptance_PropertyTax,Acceptance_PersonalTax =@Acceptance_PersonalTax,Acceptance_Tax=@Acceptance_Tax,Acceptance_TotalTax=@Acceptance_TotalTax, Lease_Id=@Lease_Id,Acceptance_TaxStartTime=@Acceptance_TaxStartTime,Acceptance_TaxEndTime=@Acceptance_TaxEndTime,Acceptance_TicketNumber=@Acceptance_TicketNumber,Acceptance_TaxNumber=@Acceptance_TaxNumber,Acceptance_State=@Acceptance_State,Acceptance_Date=@Acceptance_Date,Acceptance_UserId=@Acceptance_UserId,Acceptance_Address=@Acceptance_Address  where Acceptance_Id = @Acceptance_Id";
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, new Zor_Acceptance { Acceptance_PropertyTax = PropertyTax, Acceptance_PersonalTax = PersonalTax, Acceptance_Tax = Acceptance_Tax, Acceptance_TotalTax = Acceptance_TotalTax, Lease_Id = acceptance.Lease_Id, Acceptance_TaxStartTime = st, Acceptance_TaxEndTime = et, Acceptance_TicketNumber = acceptance.Acceptance_TicketNumber, Acceptance_TaxNumber = acceptance.Acceptance_TaxNumber, Acceptance_State = acceptance.Acceptance_State, Acceptance_Date = acceptance.Acceptance_Date, Acceptance_UserId = acceptance.Acceptance_UserId, Acceptance_Address = acceptance.Acceptance_Address, Acceptance_Id = acceptance.Acceptance_Id });
            return bar;
        }
        /// <summary>
        /// 租赁管理-新增受理单
        /// </summary>
         /// <param name="acceptance">受理单信息对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
         public static BaseAnswer InsertAcceptance(string LeaseId, int PropertyTax, int PersonalTax, int Acceptance_Tax, int Acceptance_TotalTax, string Acceptance_TaxNumber, string Acceptance_Address, DateTime startTime, DateTime endTime)
        {
            var dt = RequestSession.GetSessionUser().DT_Id;
            var sql = "select Dt_AmdCode from [dbo].[Sys_Department] where [Dt_Id]=" + dt + "";
            Sys_Department Department = DapperQuery<Sys_Department>.DoQueryT(sql, new { Dt_Id = dt });
            var TicketNumber = Department.Dt_AmdCode.ToString() + DateTime.Now.ToString("yyyyMMddhhss");
            var st = TimeHelper.ToTimeStamp(startTime);      //税起时间转时间戳格式
            var et = TimeHelper.ToTimeStamp(endTime);       //税止时间转时间戳格式
            var sql1 = @"if not exists(select Lease_Id  from [V_Acceptance] where Lease_Id=@Lease_Id and State ='已保存' ) 
                        insert into Zor_Acceptance values 
                        ( @Lease_Id,@Acceptance_TaxStartTime,@Acceptance_TaxEndTime,@Acceptance_TicketNumber ,@Acceptance_PropertyTax,@Acceptance_PersonalTax,@Acceptance_Tax ,@Acceptance_TaxNumber ,@Acceptance_State ,@User_Id ,@Acceptance_CreateTime ,@Acceptance_Date ,@Acceptance_UserId ,@Acceptance_Address ,@Dt_Id,1,@Acceptance_TotalTax)";

            BaseAnswer bar = DapperInsert<T>.DoInsert(sql1, new
            {
                Lease_Id = LeaseId,
                Acceptance_TicketNumber = TicketNumber,
                Acceptance_PropertyTax = PropertyTax,
                Acceptance_PersonalTax = PersonalTax,
                Dt_Id = dt,
                Acceptance_Type = 1,
                Acceptance_Tax = Acceptance_Tax,
                Acceptance_TotalTax = Acceptance_TotalTax,
                Acceptance_TaxNumber = Acceptance_TaxNumber,
                Acceptance_Address = Acceptance_Address,
                Acceptance_TaxStartTime = st,
                Acceptance_TaxEndTime = et,
                Acceptance_Date = 0,
                Acceptance_UserId = 0,
                Acceptance_State = 0,
                User_Id = RequestSession.GetSessionUser().User_Id,
                Acceptance_CreateTime = TimeHelper.ToTimeStamp(DateTime.Now)
            });
            return bar;
        }

         /// <summary>
         /// 删除受理单信息
         /// </summary>
         /// <param name="id">Id集合</param>
         /// <returns></returns>
         public static BaseAnswer DeleteAcceptance(int[] id)
         {
             var sql = @"if not exists(select Acceptance_Id from [Zor_Review] where Acceptance_Id in (" + string.Join(",", id).TrimEnd(',') + " ) and Review_State !=3 ) delete [Zor_Acceptance]  where  Acceptance_Id in (" + string.Join(",", id).TrimEnd(',') + ")";
                        
             BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Acceptance_Id = id.ToArray<int>() });
             return bar;
         }
         /// <summary>
         /// 查询打印区域记录
         /// </summary>
         /// <param name="dayReport_id"></param>
         /// <returns></returns>
         public static List<T> GetAcceptancePrint(int Acceptance_Id)
         {
             string sql = "select * from  [V_Acceptance] where Acceptance_Id =" + Acceptance_Id + "";
             List<T> listAcceptance = DapperQuery<T>.DoQueryList(sql, null);
             return listAcceptance;
         }
    }
}
